package cn.alusiaor.eurekaclientuserservice.controller;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Value("${cloud.config.name}")
    private String appName;

    @Value("${client.config.name}")
    private String heheName;

    @PostMapping("/hello")
    public LocalDateTime sayHello2(@RequestBody UserDto userDto) {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        log.info("我就试试");
        return userDto.getBirthday();
    }

    @GetMapping("/config")
    public String getConfig() {
        log.info("这里是我配置的内容" + appName);
        log.info("这里是我配置的内容" + heheName);
        return appName + ";" + heheName;
    }

    @PostMapping(value = "/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public File getFile(@RequestPart("file") MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        File dest = new File("D:\\dockerDirectory\\" + System.currentTimeMillis() + substring);
        try {
            file.transferTo(dest);
            return dest;
        } catch (IOException e) {
            log.error("转换错误", e);
            return null;
        }
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class UserDto {

        private String userName;
        private LocalDateTime birthday;

    }
}
